package com.admin.service;

import com.admin.dao.GoodsDao;
import com.admin.entity.Goods;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//物品信息接口类
@Service
public class GoodsService extends BaseService {

    @Autowired
    private GoodsDao goodsDao;

    //【物品信息】设置查询条件
    private LambdaQueryWrapper<Goods> getGoodsQueryCondition(Goods goods) {
        LambdaQueryWrapper<Goods> lambdaQuery = this.getBaseQueryCondition(goods);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Goods::getCreateDate);
        if (ObjectUtils.isNotNull(goods)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(goods.getId()), Goods::getId, goods.getId());
            //【物品名称】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(goods.getName()), Goods::getName, goods.getName());
            //【库位信息】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(goods.getAddr()), Goods::getAddr, goods.getAddr());
            //【供应商名称】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(goods.getSupplierId()), Goods::getSupplierId, goods.getSupplierId());
            //【物品类别】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(goods.getCategoryId()), Goods::getCategoryId, goods.getCategoryId());
            //【物品规格】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(goods.getSpecs()), Goods::getSpecs, goods.getSpecs());
            //【物品价格】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(goods.getPrice()), Goods::getPrice, goods.getPrice());
            //【预警值】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(goods.getWarnNum()), Goods::getWarnNum, goods.getWarnNum());
        }
        return lambdaQuery;
    }

    //【物品信息】分页查询
    public IPage<Goods> getPage(Page<Goods> page, Goods goods) {
        LambdaQueryWrapper<Goods> lambdaQuery = getGoodsQueryCondition(goods);
        return goodsDao.selectPage(page, lambdaQuery);
    }
    
    //【物品信息】查询列表
    public List<Goods> getList(Goods goods) {
        LambdaQueryWrapper<Goods> lambdaQuery = getGoodsQueryCondition(goods);
        return goodsDao.selectList(lambdaQuery);
    }
    
    //【物品信息】根据id查询
    public Goods get(String id) {
        return goodsDao.selectById(id);
    }

    //【物品信息】根据对象查询
    public Goods get(Goods goods) {
    LambdaQueryWrapper<Goods> lambdaQuery = getGoodsQueryCondition(goods);
        return goodsDao.selectOne(lambdaQuery);
    }
    
    //【物品信息】新增
    public int insert(Goods goods) {
        this.preInsert(goods);
        return goodsDao.insert(goods);
    }
    
    //【物品信息】修改
    public int update(Goods goods) {
        this.preUpdate(goods);
        return goodsDao.updateById(goods);
    }
    
    //【物品信息】删除
    public int delete(String id) {
        return goodsDao.deleteById(id);
    }

    //【物品信息】批量删除
    public int delAll(String[] ids) {
        return goodsDao.deleteBatchIds(Arrays.asList(ids));
    }

}